﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter.UI;

using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using RadioStream.Code.Utils;


namespace RadioStream
{
    public class CStationInfo : ModelItem
    {
        private String m_strCallSign = "";
        public String CallSign
        {
            get
            {
                return m_strCallSign;
            }
        }

        protected string m_strBannerLargeURI;
        public Microsoft.MediaCenter.UI.Image BannerLarge
        {
            get
            {
                if ("" != m_strBannerLargeURI)
                {
                    return new Microsoft.MediaCenter.UI.Image(m_strBannerLargeURI);
                }
                else return null;
            }
        }


        public CStationInfo(int StationID,int RegionID, bool bFavorite)
        {
           // set the IDs
           m_iStationID = StationID;
           m_iRegionID = RegionID;

           //retrieve Information manually
           XmlDocument doc = XMLUtil.GetXMLDocument(@"http://guide.play.it/GuideWebServices/StationGuideService2.asmx/GetStationInfoEx?noCache=null&target=null&distribution=null&stations=" + m_iStationID.ToString());
           XmlNamespaceManager nsM = new XmlNamespaceManager(doc.NameTable);
           nsM.AddNamespace("p", "http://guide.play.it/");
           //parse all information 
           m_StationLogoURL = XMLUtil.GetFirstElementText(doc, "/p:StationResult/p:StationInfo/p:StageArt", nsM);//StageArt");
           m_StationName = XMLUtil.GetFirstElementText(doc, "/p:StationResult/p:StationInfo/p:Name",nsM);
           m_strCallSign = XMLUtil.GetFirstElementText(doc, "/p:StationResult/p:StationInfo/p:CallSign", nsM);
           m_strBannerLargeURI = XMLUtil.GetFirstElementText(doc, "/p:StationResult/p:StationInfo/p:BannerExtraLarge", nsM);
           m_bFavorite = bFavorite;
           
        }

        protected String m_StationName;
        public String StationName
        {
            get
            {
                return m_StationName;
            }
        }

        protected int m_iStationID;
        public int StationID
        {
            get
            {
                return m_iStationID;
            }
        }

        protected int m_iRegionID;
        public int RegionID
        {
            get
            {
                return m_iRegionID;
            }
        }
        

        protected String m_StationLogoURL;
        protected Microsoft.MediaCenter.UI.Image m_StationLogo;
        public Microsoft.MediaCenter.UI.Image StationLogo
        {
            get
            {
                if ("" != m_StationLogoURL)
                    m_StationLogo = new Microsoft.MediaCenter.UI.Image(m_StationLogoURL);
                else
                    m_StationLogo = FileUtil.Res2Image("RadioStream.Images.no_cover.jpg");
                return m_StationLogo;
            }
            set
            {
                if (m_StationLogo != value)
                {
                    m_StationLogo = value;
                    FirePropertyChanged("StationLogo");
                }
            }

        }

        /**This class acts as a IComparer for CSongInfo objects*/
        public class StationComparer : IComparer
        {
            /**This Method return A or Z regarding the 'favorite' state of a CStationInfo
             * @param SI [in] StationInfo to check
             * @retunr "A" if station is a favorite, "Z" otherwise
            */
            private string isFav(CStationInfo SI)
            {
                string stResult = "Z";
                if (SI.IsFavorite)
                {
                    stResult = "A";
                }
                return stResult;

            }
            // Calls CaseInsensitiveComparer.Compare.
            int IComparer.Compare(object x, object y)
            {
                CStationInfo Station1 = (CStationInfo)x;
                CStationInfo Station2 = (CStationInfo)y;
                //sorting is boosted by adding a Z or an A
                int Value = ((new CaseInsensitiveComparer()).Compare((this.isFav(Station1) + Station1.StationName), (this.isFav(Station2) + Station2.StationName)));
                return Value;
            }
        }

        protected bool m_bFavorite;
        public bool IsFavorite
        {
            get
            {
                return m_bFavorite;
            }
            set
            {
                if (m_bFavorite != value)
                {
                    m_bFavorite = value;
                    FirePropertyChanged("StationFavorite");
                }
            }
        }
        public Microsoft.MediaCenter.UI.Image Favorite()
        {
            Microsoft.MediaCenter.UI.Image m_FavImage = null;
            switch (m_bFavorite)
            {
                case true:
                    m_FavImage = FileUtil.Res2Image("RadioStream.Images.star_filled.png");
                    break;
                case false:
                    m_FavImage = FileUtil.Res2Image("RadioStream.Images.star_frame.png");
                    break;
            }
            return m_FavImage;
        }

    }
}
